import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:frontend/models/address.dart';
import 'package:frontend/pages/ItemPage.dart';
import 'package:frontend/pages/addressEditPage.dart';
import 'package:frontend/states/NetState.dart';
import 'package:provider/provider.dart';

import '../models/global.dart';
import '../models/order.dart';
import '../reqs/requests.dart';
import '../widgets/expandableFab.dart';

class HistoryOrderPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HistoryOrderPageState();
  }
}

class HistoryOrderPageState extends State<HistoryOrderPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        //automaticallyImplyLeading: false,
        title: Text("历史订单记录"),
      ),
      floatingActionButton: ExpandableFab(
        distance: 112.0,
        children: [
          ActionButton(
            icon: Icon(Icons.refresh),
            onPressed: () => setState(() {}),
          ),
          ActionButton(icon: Icon(Icons.check_box_outline_blank)),
          ActionButton(icon: Icon(Icons.check_box_outline_blank)),
        ],
      ),
      body: Consumer<NetState>(builder: (context, netState, widget) {
        return FutureBuilder<List<Order>>(
            future: fetchOrders(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  return Text(snapshot.error.toString());
                }
                return Scrollbar(
                  child: ListView(
                    restorationId: 'list_demo_list_view',
                    padding: const EdgeInsets.symmetric(vertical: 8),
                    children: [
                      for (var order in snapshot.data!)
                        ListTile(
                          leading: ExcludeSemantics(
                            child: CircleAvatar(child: Text(order.good_name)),
                          ),
                          title: Text(order.good_name),
                          subtitle: Text(
                              '于${order.time_stamp}从${order.seller_name}处购买'),
                          trailing: TextButton(
                            child: const Text('详情'),
                            onPressed: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => ItemPage(
                                            itemId: order.good_id,
                                            canBuy: false,
                                          )));
                            },
                          ),
                        ),
                    ],
                  ),
                );
              } else {
                return const CircularProgressIndicator();
              }
            });
      }),
    );
  }
}
